<?php
/**
 * Created by PhpStorm.
 * User: s.utkin
 * Date: 17.10.14
 * Time: 19:33
 */

namespace Pinenut\Test;


abstract class BaseTest extends \PHPUnit_Framework_TestCase{
	public static $fixtures = [];

	protected static $fixturesIsLoaded = false;
	public static $fixturesLoadOnce = false;

	public function setUp() {
		if (!empty(static::$fixtures)) {
			$this->loadFixtures(static::$fixtures);
		}
	}

	public function loadFixtures($names) {
		if (static::$fixturesLoadOnce && static::$fixturesIsLoaded) {
			return;
		}

		foreach ((array)$names as $name) {
			FixtureManager::loadFixture($name);
		}

		static::$fixturesIsLoaded = true;
		return;
	}

	public function action($path, $params = [], $view = null, $deferred = true, $expectedException = [])
	{
		if (isset($expectedException['onRequest'])) {
			$this->setExpectedException(
				$expectedException['onRequest']['name'],
				$expectedException['onRequest']['string'],
				$expectedException['onRequest']['code']
			);
		}

		$request = new \Request($path, $params, $view, $deferred);

		if (isset($expectedException['onContext'])) {
			$this->setExpectedException(
				$expectedException['onContext']['name'],
				$expectedException['onContext']['string'],
				$expectedException['onContext']['code']
			);
		}

		$context = new \Context($request);

		if (isset($expectedException['onDispatch'])) {
			$this->setExpectedException(
				$expectedException['onDispatch']['name'],
				$expectedException['onDispatch']['string'],
				$expectedException['onDispatch']['code']
			);
		}

		$response = \Dispatcher::dispatch($context);

		return $response;
	}
} 